﻿package  {
	import flash.display.*;
	import flash.events.*;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	
	
	public class Player extends MobObj{

		private var _map:Grid;
		public var whipAngle:Number;
		public var whip:Whip;
		public var ice:Ice;
		private var myKey:Boolean;
		private var bigKey:Boolean;
		private var switch1:Boolean;
		private var switch2:Boolean;
		private var switch3:Boolean;
		private var switch4:Boolean;
		private var sprung:Boolean;
		public var grapple:Grapple;
		public var spring:Spring;
		public var sprungTime:int = 0;
		private var entranceX:int = 0;
		private var entranceY:int = 0;
		private var hasSpring:Boolean;
		private var grappling:Boolean;
		
		
		

		public function Player(aX:Number,aY:Number,aWidth:Number,aHeight:Number,aMap:Grid) {
			grappling = false;
			myKey = false;
			bigKey = false;
			sprung = false;
			hasSpring=false;
			super(aX,aY,aWidth,aHeight);
			_map = aMap;
			whip = new Whip();
			ice = new Ice(this.x,this.y,9.5,9.5,this);
			grapple = new Grapple(this.x, this.y, this);
			spring = new Spring(this.x, this.y, this);
			
			
		}
		
		//getter/setter for keys
		public function get MyKey():Boolean {return myKey;}
		public function get BigKey():Boolean{return bigKey;}
		public function get Sprung():Boolean{return sprung;}
		public function get Switch1():Boolean{return switch1;}
		public function get Switch2():Boolean{return switch2;}
		public function get Switch3():Boolean{return switch3;}
		public function get Switch4():Boolean{return switch4;}
		public function get HasSpring():Boolean{return hasSpring;}
		public function get EntranceX():int{return entranceX;}
		public function get EntranceY():int{return entranceY;}
		
		public function set MyKey(a:Boolean){myKey = a;}
		public function set BigKey(a:Boolean){bigKey = a;}
		public function set Switch1(a:Boolean){switch1 = a;}
		public function set Switch2(a:Boolean){switch2 = a;}
		public function set Switch3(a:Boolean){switch3 = a;}
		public function set Switch4(a:Boolean){switch4 = a;}
		public function set HasSpring(a:Boolean){hasSpring = a;}
		public function set EntranceX(a:int){entranceX = a;}
		public function set EntranceY(a:int){entranceY = a;}
		public function set Sprung(a:Boolean):void{sprung = a;}
		public function set Grappling(a:Boolean){grappling = a;}
		
		public override function Update(){
			if(sprung)
			{
				VelX = -4*Math.cos(whipAngle*Math.PI/180);
				VelY = -4*Math.cos(whipAngle*Math.PI/180);
				
				sprungTime++;
				if(sprungTime >= 15)
				{
					sprungTime = 0;
					sprung = false;
				}
			}
			ice.Update();
			if(grappling){
				grapple.Update();
			}
			
			spring.Update();
			super.Update();
			checkExit();
		}
		
		public function keyDown(e:KeyboardEvent)
		{
			if(!sprung)
			{
				
				switch(e.keyCode)
				{
					case Keyboard.UP :  
					{
						VelY = -2; 
						whipAngle = 270;
						break;
					}
					case Keyboard.DOWN :  
					{
						VelY = 2;
						whipAngle = 90;
						break;
					}
					case Keyboard.LEFT :  
					{
						VelX = -2;
						whipAngle = 180;
						break;
					}
					case Keyboard.RIGHT :  
					{
						VelX = 2;
						whipAngle = 0;
						break;
					}
					case Keyboard.Z :
					{
						addChild(whip);
						whip.rotation = whipAngle;
						break;
					}
					case Keyboard.X :
					{					
						ice.x = x;
						ice.y = y;
						parent.addChild(ice);
						ice.rotation = whipAngle;
						
						parent.addChild(whip);
						whip.rotation = whipAngle;
						break;
					}
					case Keyboard.C :
					{
						parent.addChild(grapple);
						grapple.x = x;
						grapple.y = y;
						grapple.rotation = whipAngle;
						grappling = true;
						break;
					}
					case Keyboard.V :
					{
						if(hasSpring)
						{
							parent.addChild(spring);
							spring.x = x+(width/2);
							spring.y = y+(height/2);
							spring.rotation = whipAngle;
						}
						
						break;
					}
				}
			
			}
			
		}
		
		public function keyUp(e:KeyboardEvent)
		{
			if(!sprung)
			{
				switch(e.keyCode)
				{
					case Keyboard.UP :  
					{
						VelY = 0; 
						break;
					}
					case Keyboard.DOWN :  
					{
						VelY = 0;
						break;
					}
					case Keyboard.LEFT :  
					{
						VelX = 0;
						break;
					}
					case Keyboard.RIGHT :  
					{
						VelX = 0;
						break;
					}
					case Keyboard.V:
					{
						if(hasSpring && spring.stage)
						parent.removeChild(spring);
						break;
					}
				}
			}
			
			
		}
		
		function checkExit()
		{
			
			if(this.x > stage.stageWidth)
			{
				x =  3
				changeRoom(1,0);
			}
			if(this.x < 0)
			{
				x =  stage.stageWidth  - 3;
				changeRoom(-1,0);
			}
			if(this.y > stage.stageHeight)
			{
				y = 3;
				changeRoom(0,1);
			}
			if(this.y < 0)
			{
				y =  stage.stageHeight  - 10;
				
				changeRoom(0,-1);
			}
			
		}
		
		public function changeRoom(modX:int = 0, modY:int = 0):void{
			
			if(_currentRoom.mapX + modX >= 0 && _currentRoom.mapY + modY >= 0)
			{
				if(_currentRoom.mapX + modX <= _map.rooms.length)
				{
					if(_currentRoom.mapY + modY <= (_map.rooms[_currentRoom.mapX + modX] as Array).length)
					{
						_map.ChangeRoom(_currentRoom.mapX,_currentRoom.mapY,modX,modY,this);
						//_currentRoom = _map.rooms[_currentRoom.mapX + modX][_currentRoom.mapY + modY];
						//tiles = _currentRoom.tiles2;
						
					}
				}
			}
			
			entranceX = x;
			entranceY = y;
			//if player x, y > room bounds
			//check if room below exists in map
			//add 1 to relevant room.mapcoords coordinate
			//if it doesn't put them back at entrance of room
			//scold them harsly
			//else
			//move them into the room
		}

	}
	
}
